
# Title: serie 1,2,4,5,7,8    				Filename: ej54.s
# Autho: Arturo Luquin Castillo           Date 04/05/2017
# Description: Numero maximo en una lista
# Input: -
# Output: serie y suma 


@@@@@@@@@@@
@Registros@
@@@@@@@@@@@

@r1 apunta a la direccion de primo
@r3 el numero actual
@r4 contador numeros
@r9 numeros vistos

.section .bss
.comm nums, 404       @ reserva espacio para numeros 4bits por numero

.section  .data

spc:                    @separarlos con +
        .ascii" , "
len= . - spc

nl:
      .ascii "\n"       @renglon de separacion
      
limit:               @indice del ultimo numero que necesitaremos.
      .long 100
 
 
 .section .text
 .globl _start
 _start:
 
 ldr r1,=nums
 mov r0, $0
 str r0, [r1]
 ldr r0,=limit
 ldr r9,[r0]
 mov r5,$0
 mov r4,$1
 mov r3, $1
 str r3,[r1, #4]!
 add r5,r5,r3

 
p0:
 add r3,r3,$1 
 add r5,r5,r3
 str r3,[r1, #4]!
 add r4,r4,$1
 add r8,r8,$1
 
p1:
 cmp r8,r9
 beq p9

p2:
 cmp r4,$2
 blt p0

p3:
 mov r4,$0
 add r3,r3,$1
 bal p0
 
 
p9:
 add r9,r9,$1     @add space for result
 str r5,[r1, #4]! @store result
 
P10:
 mov r0,$1
 mov r4,$0
 mov r5,$0
 ldr r6,=nums
 ldr r3,[r6]

printLoop:
bl print_num		@ function call
add r4, $1		@ add one to temp counter
add r5, $1		@ add one to counter
cmp r5, r9 		@ are we done?
bge exit		@ if so, exit
cmp r4, $9		@ after 10 primes ...
bgt newline		@ print a newline
ble space		@ add spaces

space:                  @ we jump here if
mov r0, $1              @ we are going to 
ldr r1, =spc		@ print spaces
ldr r2, =len	
mov r7, $4
svc $0
ldr r3, [r6, #4]!	@ load next prime
bal printLoop           @ continue printing

newline:                @ we jump here if
mov r0, $1              @ we are going to
ldr r1, =nl             @ print a newline
mov r2, $1
mov r7, $4
svc $0
ldr r3, [r6, #4]!       @ load next prime
mov r4, $0              @ reset temporary counter
bal printLoop		@ continue printing

@@@@@@@@@@@@@@@@@@@@@@
@ print_num function @
@@@@@@@@@@@@@@@@@@@@@@

print_num:
	stmfd sp!, {r0-r9, lr}	@ push regs to stack
	mov r4, $0 		@ set division counter to zero
	mov r5, $1		@ set char counter to one

loop:				@ division routine
	cmp r3, $9		
	ble stackPush		@ if r3 <= 9, call stackPush
	sub r3, r3, $10		@ else, subtract 10 from r3
	add r4, r4, $1		@ add one to div. counter
	bal loop		@ repeat

stackPush:
	add r5, r5, $1		@ increment char counter
	orr r0, r3, $0x30	@ logical OR - add 48 to digit to get ascii code
	stmfd	sp!, {r0}	@ push onto stack
	cmp r4, $0		@ if the div. counter is zero ...
	beq printChars		@ call print function
	mov r3, r4		@ else, load div. count into r3
	mov r4, $0		@ reset div. counter
	bal loop		@ back to top of loop

printChars:
	mov r1, sp		@ use stack pointer to provide ascii code
	mov r0, $1		@ stdout is file descriptor 1
	mov r2, $1		@ length to print is 1
	mov r7, $4		@ write syscall
	svc $0			@ wake kernel
	subs r5, r5, $1		@ decrement string counter and set flag
	ble return		@ return if done
	ldmfd sp!, {r0}		@ pull next char from from stack 
	bal printChars		@ get next char
return:
	ldmfd sp!, {r0-r9, pc}	@ restore registers

exit:
mov r0, $1			@ print a newline
ldr r1, =nl
mov r2, $1
mov r7, $4
svc $0
mov r7, $1			@ exit
svc $0

.end
